﻿namespace MicroCloud.Utils
{
    /// <summary>
    /// 值生成特性
    /// <para>放在类上表示为实体主键属性值生成特性</para>
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
    public sealed class ValueGenerateAttribute : Attribute
    {
        #region 构造函数
        /// <summary>
        /// 初始化一个值生成特性 <see cref="ValueGenerateAttribute"/> 的新实例
        /// </summary>
        /// <param name="generateMode">数据库字段值生成方式</param>
        public ValueGenerateAttribute(ValueGenerateMode generateMode = ValueGenerateMode.Snowflake)
            : this(ValueGenerateRule.Never, generateMode)
        { }
        
        /// <summary>
        /// 初始化一个值生成特性 <see cref="ValueGenerateAttribute"/> 的新实例
        /// </summary>
        /// <param name="generateRule">数据库字段值生成规则</param>
        public ValueGenerateAttribute(ValueGenerateRule generateRule = ValueGenerateRule.Never)
            : this(generateRule, ValueGenerateMode.Snowflake)
        { }

        /// <summary>
        /// 初始化一个值生成特性 <see cref="ValueGenerateAttribute"/> 的新实例
        /// </summary>
        /// <param name="generateRule">数据库字段值生成规则</param>
        /// <param name="generateMode">数据库字段值生成方式</param>
        public ValueGenerateAttribute(ValueGenerateRule generateRule = ValueGenerateRule.Never, ValueGenerateMode generateMode = ValueGenerateMode.Snowflake)
            : this(generateRule, generateMode, false)
        { }

        /// <summary>
        /// 初始化一个值生成特性 <see cref="ValueGenerateAttribute"/> 的新实例
        /// </summary>
        /// <param name="generateMode">数据库字段值生成方式</param>
        /// <param name="forced">强制的：true - 数据新增时一律按规则重新生成，false - 数据新增时若值不存在则按规则生成。</param>
        public ValueGenerateAttribute(ValueGenerateMode generateMode = ValueGenerateMode.Snowflake, bool forced = false)
        : this(ValueGenerateRule.Never, generateMode, forced)
        { }

        /// <summary>
        /// 初始化一个值生成特性 <see cref="ValueGenerateAttribute"/> 的新实例
        /// </summary>
        /// <param name="generateRule">数据库字段值生成规则</param>
        /// <param name="generateMode">数据库字段值生成方式</param>
        /// <param name="forced">强制的：true - 数据新增时一律按规则重新生成，false - 数据新增时若值不存在则按规则生成。</param>
        public ValueGenerateAttribute(ValueGenerateRule generateRule = ValueGenerateRule.Never, ValueGenerateMode generateMode = ValueGenerateMode.Snowflake, bool forced = false)
        {
            GenerateRule = generateRule;
            GenerateMode = generateMode;
            Forced = forced;
        }

        #endregion

        #region 属性
        /// <summary>
        /// 获取 数据库字段值生成规则
        /// </summary>
        public ValueGenerateRule GenerateRule { get; }

        /// <summary>
        /// 获取 数据库字段值生成方式
        /// </summary>
        public ValueGenerateMode GenerateMode { get; }
 
        /// <summary>
        /// 获取 强制的
        /// <para>true：数据新增时一律按规则重新生成。</para>
        /// <para>false：数据新增时若值不存在则按规则生成。</para>
        /// </summary>
        public bool Forced { get; } = false;

        #endregion
    }
}